package com.runa.protocol.enums;

public enum ModbusDataType {

    Modbus_UTF8("UTF8"),
    Modbus_Boolean("布尔"),
    Modbus_Bit("bit"),
    Modbus_Int8("8位整型"),
    Modbus_UInt8("8位无符号整型"),
    Modbus_Int16("16位整型"),
    Modbus_UInt16("16位整型无符号"),
    Modbus_Int32("32位整型"),
    Modbus_UInt32("32位整型无符号"),
    Modbus_Float("浮点"),
    Modbus_Double("双精度浮点");

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    ModbusDataType(String value) {
        this.value = value;
    }

    public static ModbusDataType valuesOf(String typeName) throws Exception {
        for (ModbusDataType modbusDataType : values()) {
            if (modbusDataType.value.equals(typeName)) {
                return modbusDataType;
            }
        }
        throw new Exception("ModbusDataType err: " + typeName);
    }

    public int getRegisterCount() {
        switch (this) {
            case Modbus_Bit:
            case Modbus_Boolean:
            case Modbus_Int16:
            case Modbus_UInt16:
                return 1;
            case Modbus_Int32:
            case Modbus_UInt32:
            case Modbus_Float:
                return 2;
            case Modbus_Double:
                return 4;
            default:
                break;
        }
        return 0;
    }

    public int getByteCount() {
        switch (this) {
            case Modbus_Int8:
            case Modbus_UInt8:
            case Modbus_Boolean:
                return 1;
            case Modbus_Int16:
            case Modbus_UInt16:
                return 2;
            case Modbus_Int32:
            case Modbus_UInt32:
            case Modbus_Float:
                return 4;
            case Modbus_Double:
                return 8;
        }
        return 0;
    }
}
